package com.zhonghan.recharge.service.impl;
import java.util.Date;
import java.util.List;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhonghan.common.bean.Result;
import com.zhonghan.common.enums.ResultStatus;
import com.zhonghan.common.wechat.api.MessageApi;
import com.zhonghan.recharge.dao.WechatReplyDao;
import com.zhonghan.recharge.pojo.WechatReply;
import com.zhonghan.recharge.service.WechatReplyService;
@Service
public class WechatReplyServiceImpl implements WechatReplyService{
	
	private Logger logger = Logger.getLogger(this.getClass());
	
    @Autowired
    private WechatReplyDao wechatReplyDao;
    @Override
    public long getWechatReplyRowCount(){
        return wechatReplyDao.getWechatReplyRowCount();
    }
    @Override
    public List<WechatReply> selectWechatReply(){
        return wechatReplyDao.selectWechatReply();
    }
    @Override
    public List<WechatReply> selectWechatReplyByObj(WechatReply obj){
        return wechatReplyDao.selectWechatReplyByObj(obj);
    }
    @Override
    public WechatReply selectWechatReplyById(Integer id){
        return wechatReplyDao.selectWechatReplyById(id);
    }
    @Override
    public int insertWechatReply(WechatReply value){
        return wechatReplyDao.insertWechatReply(value);
    }
    @Override
    public int insertNonEmptyWechatReply(WechatReply value){
        return wechatReplyDao.insertNonEmptyWechatReply(value);
    }
    @Override
    public int insertWechatReplyByBatch(List<WechatReply> value){
        return wechatReplyDao.insertWechatReplyByBatch(value);
    }
    @Override
    public int deleteWechatReplyById(Integer id){
        return wechatReplyDao.deleteWechatReplyById(id);
    }
    @Override
    public int updateWechatReplyById(WechatReply enti){
        return wechatReplyDao.updateWechatReplyById(enti);
    }
    @Override
    public int updateNonEmptyWechatReplyById(WechatReply enti){
        return wechatReplyDao.updateNonEmptyWechatReplyById(enti);
    }

	@Override
	public Result saveSubscribeReply(String content) {
		Result result = new Result();
		try {
			
			WechatReply obj = new WechatReply();
			obj.setEventType(MessageApi.MESSAGE_SUBSCRIBE); // 查询关注公众号消息
			List<WechatReply> replyList = wechatReplyDao.selectWechatReplyByObj(obj );
			int rows = 0;
			if(replyList != null && replyList.size()>0){
				WechatReply wechatReply = replyList.get(0);
				wechatReply.setContent(content);
				wechatReply.setMsgType(MessageApi.MESSAGE_TEXT);
				wechatReply.setCreateTime(new Date());
				rows = wechatReplyDao.updateNonEmptyWechatReplyById(wechatReply);
				
			}else{
				WechatReply value = new WechatReply();
				value.setContent(content);
				rows = wechatReplyDao.insertNonEmptyWechatReply(value);
			}
			if(rows>0){
				result.setCode(ResultStatus.SUCCESS.getCode());
				result.setMsg("关注公众号自动回复消息保存成功!");
				logger.info("关注公众号自动回复消息保存成功!");
			}else{
				result.setCode(ResultStatus.FAILURE.getCode());
				result.setMsg("关注公众号自动回复消息保存失败!");
				logger.info("关注公众号自动回复消息保存失败!");
			}
			
		} catch (Exception e) {
			result.setCode(ResultStatus.BUSYNESS.getCode());
			result.setMsg(ResultStatus.BUSYNESS.getDescription());
			logger.info("关注公众号自动回复消息保存失败!");
			e.printStackTrace();
		}
		
		return result;
	}
	
	@Override
	public Result saveKeywordsReply(String keywords, String content) {
		Result result = new Result();
		
		try {
			WechatReply obj = new WechatReply();
			obj.setKeywords(keywords);
			List<WechatReply> keywordsList = wechatReplyDao.selectWechatReplyByObj(obj);
			int rows = 0;
			if(keywordsList != null && keywordsList.size()>0){
				WechatReply wechatReply = keywordsList.get(0);
				wechatReply.setContent(content);
				wechatReply.setCreateTime(new Date());
				rows = wechatReplyDao.updateWechatReplyById(wechatReply);
			}else{
				obj.setContent(content);
				obj.setMsgType(MessageApi.MESSAGE_TEXT);
				rows = wechatReplyDao.insertNonEmptyWechatReply(obj);
			}
			if(rows>0){
				result.setCode(ResultStatus.SUCCESS.getCode());
				result.setMsg("添加关键字回复消息保存成功!");
				logger.info("添加关键字回复消息保存成功!");
			}else{
				result.setCode(ResultStatus.FAILURE.getCode());
				result.setMsg("添加关键字回复消息保存失败!");
				logger.info("添加关键字回复消息保存失败!");
			}
			
		} catch (Exception e) {
			result.setCode(ResultStatus.BUSYNESS.getCode());
			result.setMsg(ResultStatus.BUSYNESS.getDescription());
			logger.info("添加关键字回复消息保存失败!");
			e.printStackTrace();
		}
		
		
		return result;
	}
	
	@Override
	public Result getKeywordsReply() {
		Result result = new Result();
		List<WechatReply> list = wechatReplyDao.selectWechatReplyByKeywords();
		if(list != null && list.size() > 0){
			result.setCode("1");
			result.setData(list);
		}
		return result;
	}

}